//
//  JiluListVC.m
//  SSC
//
//  Created by 红沙尘 on 17/6/11.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "JiluListVC.h"
#import "RMDateSelectionViewController.h"

@interface JiluListVC ()<RMDateSelectionViewControllerDelegate>
{
    __weak IBOutlet UIButton *searchBtn;
    
    __weak IBOutlet UIView *sView;
    __weak IBOutlet UIView *eView;
    
    __weak IBOutlet UILabel *noLabel;
    __weak IBOutlet UILabel *countLabel;
}

@property (nonatomic, strong) RMDateSelectionViewController *startDateVC;
@property (nonatomic, strong) RMDateSelectionViewController *endDateVC;

@property (nonatomic, copy) NSDate *startDate;
@property (nonatomic, copy) NSDate *endDate;


@end

@implementation JiluListVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    typeLabel.text = self.title;
    
    if (dataArray.count == 0) {
        page = 1;
        [mainTableView.mj_footer beginRefreshing];
    } else {
        [self reloadRequest];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"投注记录"];
}

#pragma mark - IBAction

- (IBAction)startDateSelect {
    
    if (!self.startDateVC) {
        self.startDateVC = [RMDateSelectionViewController dateSelectionController];
        self.startDateVC.delegate = self;
        self.startDateVC.disableBouncingWhenShowing = NO;
        self.startDateVC.disableMotionEffects       = YES;
        self.startDateVC.disableBlurEffects         = YES;
        self.startDateVC.blurEffectStyle = UIBlurEffectStyleDark;
        [self.startDateVC.datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+8"]];
        [self.startDateVC.datePicker setDatePickerMode:UIDatePickerModeDate];
    }
    
    self.startDateVC.datePicker.maximumDate = self.endDate;
    
    [self.startDateVC show];
}

- (IBAction)endDateSelect {
    
    if (!self.endDateVC) {
        self.endDateVC = [RMDateSelectionViewController dateSelectionController];
        self.endDateVC.delegate = self;
        self.endDateVC.disableBouncingWhenShowing = NO;
        self.endDateVC.disableMotionEffects       = YES;
        self.endDateVC.disableBlurEffects         = YES;
        self.endDateVC.blurEffectStyle = UIBlurEffectStyleExtraLight;
        [self.endDateVC.datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+8"]];
        self.endDateVC.datePicker.maximumDate = [NSDate date];
        
        [self.endDateVC.datePicker setDatePickerMode:UIDatePickerModeDate];
    }
    
    self.endDateVC.datePicker.minimumDate = self.startDate;
    [self.endDateVC show];
    
}

- (void)loadWithArray:(NSArray *)array {
    
    if (page == 1) [dataArray removeAllObjects];
    
    [dataArray addObjectsFromArray:array];
    
    [mainTableView reloadData];
    
    noLabel.hidden = dataArray.count > 0 ? YES : NO;

    countLabel.text = [NSString stringWithFormat:@"共%d条",(int)dataArray.count];
    
    if (array.count > 0) page ++;
}

- (IBAction)searchButtonPressed {
    [self reloadRequest];
}

#pragma mark - 子类重写

- (void)reloadRequest {
    page = 1;
    [self loadData];
}

- (void)loadData {
    noLabel.hidden = YES;
}

#pragma mark - table

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JiluListCell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"JiluListCell"];
    }
    
    return cell;
}

#pragma mark - RMDateSelectionViewControllerDelegate

- (void)dateSelectionViewController:(RMDateSelectionViewController *)vc
                      didSelectDate:(NSDate *)aDate {
    
    if (vc == self.startDateVC) {
        self.startDate = aDate;
        
    } else if (vc == self.endDateVC) {
        self.endDate = aDate;
    }
}

#pragma mark - noLabel

- (UILabel *)noLabel {
    return noLabel;
}

#pragma mark - setup

- (void)setupData {

    dataArray = [[NSMutableArray alloc] init];

    self.startDate = [NSDate date];
    self.endDate   = [NSDate date];

    WeakSelf
    
    mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadData];
    }];

    mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf reloadRequest];
    }];
}

- (void)setupUI {

    searchBtn.layer.cornerRadius  = 2;
    searchBtn.layer.masksToBounds = YES;
    
    sView.layer.cornerRadius  = 2;
    sView.layer.masksToBounds = YES;
    sView.layer.borderColor   = COLOR_CCCCCC.CGColor;
    sView.layer.borderWidth   = LineHeight;
    
    eView.layer.cornerRadius  = 2;
    eView.layer.masksToBounds = YES;
    eView.layer.borderColor   = COLOR_CCCCCC.CGColor;
    eView.layer.borderWidth   = LineHeight;
    
    startField.leftView     = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    startField.leftViewMode = UITextFieldViewModeAlways;
    endField.leftView       = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    endField.leftViewMode   = UITextFieldViewModeAlways;
    
    [mainTableView registerNib:[UINib nibWithNibName:@"TouzhujiluCell" bundle:nil]
        forCellReuseIdentifier:@"TouzhujiluCell"];
    mainTableView.rowHeight = 130;
    mainTableView.estimatedRowHeight = 130;
    
}

- (void)setStartDate:(NSDate *)startDate {
    
    if (_startDate != startDate) {
        
        _startDate = nil;
        _startDate = [startDate copy];

        startField.text = [MTool dateToString:_startDate];
    }
}

- (void)setEndDate:(NSDate *)endDate {
    
    if (_endDate != endDate) {
        _endDate = nil;
        _endDate = [endDate copy];

        endField.text = [MTool dateToString:_endDate];
    }
}


@end





